/**
 * @class   wosh::building::BuildingGeometry
 * @brief   
 *
 *



 *
 *
 * References:
 *  - http://www.cs.princeton.edu/courses/archive/spr06/cos426/lectures/12-reps.pdf
 *  - http://www.cs.princeton.edu/courses/archive/fall02/cs526/lectures/reps.pdf
 *
 *
 * @see     wosh::building::BuildingObject
 ****************************************************************************
 * @version 0.8.151 $Id: BuildingGeometry.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Building_BuildingGeometry_H__
 #define __WOSH_Building_BuildingGeometry_H__

 #include <woshDefs.h>
 #include <core/Serialization.h>



namespace wosh {
 namespace building {

struct Point3D {
	double x; ///< width
	double y; ///< height
	double z;
 };

struct Voxel {
	short type;		// { empty, fill, shadow }
	short material;
 };

class BuildingGeometry : public virtual ISerializable
 {
	WOSH_SERIALIZABLE(wosh::building::BuildingGeometry)
	WOSH_COMPARE_RTTI(wosh::building::BuildingGeometry)

	public:
		enum GEOMETRY_MODEL {
			MODEL_UNKNOWN		= 0x0000,
			MODEL_2D_RECT		= 0x0001,
			MODEL_2D_SQUARE		= 0x0003,
			MODEL_2D_POLYGON	= 0x0004,
			MODEL_2D_CIRCLE		= 0x0008,

			MODEL_3D_PRISM_RECT	= 0x0101,
			MODEL_3D_CUBE		= 0x0103,
			MODEL_3D_SPHERE		= 0x0104
		 };

	public:
		/**
		 * @brief  Empty constructor. Init vars.
		 */
		BuildingGeometry();
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		BuildingGeometry( const BuildingGeometry& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~BuildingGeometry() { }


/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const Point3D& getPosition() const		{ return this->position; }
		Point3D& getPosition()					{ return this->position; }

		const Point3D& getBoundingBox() const	{ return this->boundingBox; }
		Point3D& getBoundingBox()				{ return this->boundingBox; }

		bool isZero() const;
		bool is2D() const;
		bool is3D() const;

//		const Voxel** getVolumetricGrid() const	{ return this->vortexGrid; }
		Voxel** getVolumetricGrid()				{ return this->vortexGrid; }

		double getVolumetricSampleSize() const	{ return this->vortexSize; }

		static const char* getModelAsString( GEOMETRY_MODEL model );

		static GEOMETRY_MODEL getModelFromString( const char* model );

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setPosition( const Point3D& position );
		void setPosition( double x, double y, double z = 0.0 );

		void setBoundingBox( const Point3D& bb );
		void setBoundingBox( double size_x, double size_y, double size_z = 0.0 );

		virtual WRESULT calculateBoundingBox();

		inline void setVolumetricSampleSize( double voxel_size )	{ this->vortexSize = voxel_size; }

		virtual void autobuildVortexGrid();
		virtual void rebuildVortexGrid();
		virtual void allocVortexGrid( int x, int y, int z );

		virtual void clear();

		virtual WRESULT buildGridFromModel( GEOMETRY_MODEL model, const double* values, int value_count, double rotation = 0.0 );

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual BuildingGeometry& operator=(const BuildingGeometry& m);

		virtual bool operator==(const BuildingGeometry &other) const;
		virtual bool operator!=(const BuildingGeometry &other) const { return !(*this == other);  }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:
		Point3D position;		///< position relative to parent
		Point3D boundingBox;	///< size of bounding box, relative to (0,0,0)

		Voxel** vortexGrid;		///< matrix of vortex: array of Z LAYERS, each layer has size [X,Y]
		int vortexGridX;
		int vortexGridY;
		int vortexGridZ;
		double vortexSize;		///< size of the vortex (a cube), linked to boundingBox and vortexGrid

}; // class def

 }; // namespace building
}; // namespace wosh

#endif //__WOSH_Building_BuildingGeometry_H__
